package org.gurms.service.system.impl;

import java.util.List;

import org.gurms.common.exception.GurmsServiceException;
import org.gurms.common.util.ReflectionUtil;
import org.gurms.dao.po.system.SysParam;
import org.gurms.dao.system.SysParamDao;
import org.gurms.service.dto.Page;
import org.gurms.service.dto.PropertyFilter;
import org.gurms.service.dto.system.ParamDto;
import org.gurms.service.system.SysParamService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class SysParamServiceImpl implements SysParamService{

	private Logger logger = LoggerFactory.getLogger(SysParamServiceImpl.class);

	private static final String VALID_YES = "1"; //有效
	private static final String VALID_NO  = "0"; //无效
	
	@Autowired
	private SysParamDao sysParamDao;

	public SysParamDao getSysParamDao() {
		return sysParamDao;
	}

	public void setSysParamDao(SysParamDao sysParamDao) {
		this.sysParamDao = sysParamDao;
	}

	@Transactional(readOnly = true)
	public void save(ParamDto dto) {
		SysParam param = getSysParamFromDto(dto);
		sysParamDao.save(param);
	}

	@Transactional(readOnly = true)
	public void update(ParamDto dto) {
		SysParam param = getSysParamFromDto(dto);
		sysParamDao.update(param);
	}

	public String getParamValue(String key){
		SysParam param = sysParamDao.get(key);
		if(param!=null && VALID_YES.equals(param.getParamstatus())){
			return param.getParamvalue();
		}else{
			logger.error("[" + key + "] not exists");
			throw new GurmsServiceException("[" + key + "] not exists");
		}		
	}
	
	protected SysParam getSysParamFromDto(ParamDto dto){
		SysParam param = (SysParam)ReflectionUtil.getDozer().map(dto, SysParam.class);
		return param;
	}

	public void delete(ParamDto id) {
		// TODO Auto-generated method stub
		
	}

	public ParamDto get(ParamDto id) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page<ParamDto> getAll(Page<ParamDto> page) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page<ParamDto> search(Page<ParamDto> page,
			List<PropertyFilter> filters) {
		// TODO Auto-generated method stub
		return null;
	}
}
